﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CatSentenceAnalyzer
{
    public class CATZHCNSeg : CLangSegment
    {
        
        public CATZHCNSeg(){
            m_type = 2;
        }


       override  public int GetSentenceSumKey()
        {
            string tmp = ""; int sum = 0;
            GetRegexCollection();
            
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                if (m_sentencecoll[i].Groups.Count > 0)
                {
                    tmp += m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;   
                }
            }
            sum = GetSumKey(tmp);
            if (sum == 0)
            {
                sum = SumKey(tmp);
                putHashKey(sum.ToString());
            }
            else
            {
                putKey(sum);
            }
            return sum;
        }


        override public ArrayList GetMatchCollection3()
        {
            if (m_sentencecoll == null)
                GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++ )
            {
                string tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                int ilen = tmp.Length;
                if (ilen >= MINLEN*2)
                {
                    string s1, s2,s3;
                    int mod = ilen % 3;
                    if ( mod == 0)//偶数
                    {
                        mod = ilen / 3;
                        s1 = tmp.Substring( 0, mod);
                        s2 = tmp.Substring(mod , mod );
                        s3 = tmp.Substring(mod * 2);

                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));
                    }else{//奇数
                        mod = ilen / 3;
                        s1 = tmp.Substring(0, mod);
                        s2 = tmp.Substring(mod, mod);
                        s3 = tmp.Substring(mod * 2);
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));

                        s1 = tmp.Substring(0, mod+1);
                        s2 = tmp.Substring(mod+1, mod +1);
                        s3 = tmp.Substring(mod * 2+1);
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        putKey(GetSumKey(s3));
                    }
                }else{
                    putKey(GetSumKey(tmp));
                }
            }
            return m_MatchSumKey;
        }

        override public ArrayList GetMatchCollection2()
        {
            if (m_sentencecoll == null)
                GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                string tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                int ilen = tmp.Length;
                if (ilen > MINLEN)
                {
                    string s1, s2;
                    int mod = ilen % 2;
                    if (mod == 0)//偶数
                    {
                        mod = ilen / 2;
                        s1 = tmp.Substring(0, mod);
                        s2 = tmp.Substring(mod);
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                    }
                    else
                    {//奇数
                        mod = ilen / 2;
                        s1 = tmp.Substring(0, mod);
                        s2 = tmp.Substring(mod);
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                        s1 = tmp.Substring(0, mod + 1);
                        s2 = tmp.Substring(mod + 1);
                        putKey(GetSumKey(s1));
                        putKey(GetSumKey(s2));
                    }
                }
                else
                {
                    putKey(GetSumKey(tmp));
                }
            }
            return m_MatchSumKey;
        }
        
        override public ArrayList GetMatchCollection1()
        {
            if ( m_sentencecoll == null)
            {
                GetRegexCollection();
            }
            for (int i = 0; i < m_sentencecoll.Count; i++ )
            {
                string tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                int sum = GetSumKey(tmp);
                putKey(sum);
            }
            return m_MatchSumKey;
        }

        override public int GetDiectoryIndexKey()
        {
            string tmp = ""; int sum = 0;
            GetRegexCollection();

            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                if (m_sentencecoll[i].Groups.Count > 0)
                {
                    tmp = m_sentencecoll[i].Groups[m_sentencecoll[i].Groups.Count - 1].Value;
                    break;
                }
            }
            sum = GetSumKey(tmp);
            putKey(sum);
            return sum;
        }
        override public ArrayList GetTdbCollection()
        {
            string tmp = "";
            //if (m_sentencecoll == null)
            GetRegexCollection();
            for (int i = 0; i < m_sentencecoll.Count; i++)
            {
                tmp = m_sentencecoll[i].Groups[0].Value;
                putKey(GetSumKey(tmp));
            }
            return m_MatchList;
        }
    }
}
